跳到主要内容

角色攻击冷却设定

以下是个简单的承受伤害,使用计时器冷却避免用户可以一直连续攻击,例如下面的例子,第一次收到伤害时,使用 can_tak_damage 设置成 false,并且开启计时器,这样在计时器内用户都无法攻击了

func deal_with_damage():
if player_inattack_zone and global.player_current_attack:
if can_tak_damage:
$take_damage_cooldown_timer.start() # 启动伤害冷却计时器
can_tak_damage = false
health -= 20
print("史莱姆受到伤害,剩余生命值:", health)
if health <= 0:
self.queue_free() # 销毁敌人节点


func _on_take_damage_cooldown_timer_timeout() -> void:
can_tak_damage = true